//index.js
//获取应用实例
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        cwidth: 750,
        cheight: 1206,
        bgimgurl:"http://orbh5la4g.bkt.clouddn.com/123.jpg",
        savePicAuthorize:true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        // this.saveImageFilePloto();
    },

    /**
     * @事件绑定
     */
    saveImageFilePloto() {
        let that = this;
        wx.showLoading({
            title: '绘制中',
            mask: true,
        })
        if (!that.data.saveImageFile) {
            wx.downloadFile({
                url: that.data.bgimgurl,
                success(res) {
                    console.log('背景下载成功')
                    console.log(res)
                    var bg = res.tempFilePath;
                    that.drawHaibai(bg);
                },
                fail(res) {
                    wx.showToast({
                        title: '下载图片失败，请稍后重试',
                        icon: 'none',
                    })
                },
            })
        } else {
            that.authorizeSaveImageCheck();
        }
    },

    /**
     * 绘制海报图
     */
    drawHaibai(bg) {
        let that = this;
        console.log('开始绘制海报图')
        console.log(app.data.userInfo)
        var ctx = wx.createCanvasContext('haibai-canvas');
        ctx.width = that.data.cwidth;
        ctx.height = that.data.cheight;
        var cwidth = that.data.cwidth,
            cheight = that.data.cheight;
        var bg = bg, 
            codeimg = '../images/codesimg.jpg',
            text1 = '长按二维码',
            text2 = '抽取自己的七夕姻缘签';
        drawMethod();

        function drawMethod() {
            ctx.drawImage(bg, 0, 0, cwidth / 2, cheight / 2);
            //文字描述
            ctx.setFontSize(14);
            ctx.setFillStyle('#ffffff');
            ctx.fillText(text1, (cwidth / 2) / 2 - 35, cheight / 2 - 70);
            ctx.fillText(text2, (cwidth / 2) / 2 - 70, cheight / 2 - 50);

            /**
             * 二维码，绘制逻辑：先绘制一个圆形背景，接着将二维码绘制到圆形内并进行裁剪
             */
            ctx.save();
            ctx.beginPath();
            ctx.arc((cwidth / 2) / 2, cheight / 2 - 135, 40, 0, 2 * Math.PI);
            ctx.setFillStyle('#eeeeee');
            ctx.fill();
            ctx.clip();
            ctx.drawImage(codeimg, (cwidth / 2) / 2 - 38, cheight / 2 - 173, 150 / 2, 150 / 2);
            ctx.restore();
            //draw是异步，需在回调后再执行导出画布资源的逻辑
            ctx.draw(false, (ret) => {
                console.log('绘制完成')
                wx.hideLoading();
                that.canvasToImageFile();
            })
        }
    },

    /**
     * @画布导出图片
     * canvas导出图片
     */
    canvasToImageFile: function(res) {
        let that = this;
        wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: that.data.cwidth * 2,
            height: that.data.cheight * 2,
            canvasId: 'haibai-canvas',
            success: function(res) {
                that.data['saveImageFile'] = res.tempFilePath;
                that.authorizeSaveImageCheck();
            },
            fail: function(err) {

            }
        })
    },


    /**
     * 打开设置页面后的回调
     */
    bindopensetting() {
        let that = this;
        console.log('打开设置页面回调')
        wx.showLoading({
            title: '请稍后',
            mask: true,
        })
        that.authorizeSaveImageCheck();
    },

    /**
     * @授权
     * 保存图片到相册授权
     */
    authorizeSaveImageCheck() {
        let that = this;
        wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success: function(res) {
                console.log(res)
                wx.showLoading({
                    title: '保存中...',
                    mask: true,
                })
                that.setData({
                    savePicAuthorize: true
                })
                that.saveImageToPhotosAlbum();
            },
            fail: function(res) {
                console.log('授权拒绝')
                wx.hideLoading();
                that.setData({
                    savePicAuthorize: false
                })
            },
        })
    },

    /**
     * @保存操作
     * 保存图片到用户本地
     */
    saveImageToPhotosAlbum: function() {
        let that = this;
        wx.saveImageToPhotosAlbum({
            filePath: that.data.saveImageFile,
            success(res) {
                wx.hideLoading();
                wx.showModal({
                    content: '海报图已保存至相册，快去发朋友圈吧',
                    showCancel: false,
                    confirmText: '知道了',
                })
                setTimeout(function() {
                    wx.hideToast()
                }, 2000)
            },
            fail: function(res) {
                wx.hideLoading();
            }
        })
    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },
})